const request=require("./request")
const login=require('./login');
const common=require('./common');
/**
 * 从微信用户侧进行授权读取用户资料
 * @param {string} desc 
 */
const getUserInfoByWx=async (desc)=>{
  return new Promise((resolve,reject)=>{
    wx.getUserProfile({
      desc: desc, // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        switch(res.userInfo.gender){
          case 0:
            res.userInfo.genderName='未知';
            break;
            case 1:
              res.userInfo.genderName='男';
              break;
            case 2:
              res.userInfo.genderName='女';
              break;
        }
        wx.setStorageSync('userInfo', res.userInfo)
        resolve(res.userInfo)
      },
      fail:(e)=>{
        console.error('获取用户信息出错，可能用户未授权',e)
        reject(common.error("获取用户信息出错，可能用户未授权。",-1));
      }
    });
  });
}
/**
 * 读取应用本地缓存用户资料
 */
const getUserInfoByLocal=async ()=>{
  let userInfoStorage= wx.getStorageSync('userInfo')
  if(userInfoStorage){
    return Promise.resolve(userInfoStorage)
  }else{
    return Promise.reject(common.error("获取本地缓存用户失败",-1))
  }
}
/**
 * 读取服务器上用户资料
 */
const getUserInfoByRemote=async ()=>{
  return new Promise((resolve,reject)=>{
    request.yunrequest("wxv2","getUserInfo",{}).then(
      success=>{
        console.log(success)
        if(success.userInfo){
          wx.setStorageSync('userInfo', success.userInfo)
          resolve(success.userInfo)
        }else{
          console.log("服务器获取用户信息失败",success.errorMsg)
          reject(common.error(success.errorMsg,-1))
        }
      },err=>{
        reject(err)
      }
    )
  })
}
/**
 * 更新用户资料，同时保存到服务器及缓存
 * @param {string} desc  
 */
const updateUserInfo=async(desc)=>{
  return new Promise((resolve,reject)=>{
    getUserInfoByWx(desc).then(
      success=>{
        request.yunrequest("wxv2","updateUserInfo",{userInfo:success})
        resolve(success)
      },
      err=>{
        return reject(err)
      })
  })
}
/**
 * 获取用户资料
 * @param {string} desc  提示文字
 * @param {boolean} forceUpdate 是否强制更新，注意只能通过用户进行触发 
 */
async function getUserInfo(desc,forceUpdate){
  return new Promise((resolve,reject)=>{
    login.getJwtPayload().then((success)=>{
      if(forceUpdate){
        updateUserInfo(desc).then(res=>{
          console.log("update",res)
          resolve(res)
          return;
        },err=>{
          console.log("update",err)
          reject(err)
          return;
        })
        return;
      }
      getUserInfoByLocal().then(userInfo=>{
        console.log("local",userInfo)
        resolve(userInfo)
        return;
      },err=>{
        getUserInfoByRemote().then(
          success=>{
            console.log("Remote",success)
            resolve(success)
          },err=>{
            console.log("Remote",err)
            reject(err)
          }
        )
      })
    },
    err=>{
      reject(err)
    });
  })
}
/**
 * 获取用户昵称
 * @param {string} desc  提示文字
 * @param {boolean} forceUpdate 是否强制更新，注意只能通过用户进行触发 
 * @returns {Promise<string>} 用户昵称
 */
async function getNickName(desc,forceUpdate){
  return new Promise((resolve,reject)=>{
    return getUserInfo(desc,forceUpdate).then(
      (success)=>{
        console.log(success)
        resolve(success.nickName)
      },
      err=>{
        reject(err)
      }
    );
  })
}
module.exports = {
  getUserInfo: getUserInfo,
  getNickName:getNickName
}